package com.caterina.activities;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JScrollPane;
import person.Employee;
import person.User;

import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.composite.MUser;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CActivityButton;
import com.caterina.components.COptionPane.MessageType;
import com.caterina.components.CPanel;
import com.caterina.resources.CWords;

import contract.EmployeeContract;
import controllers.Checks;
import controllers.ExceptionsTypes;
import controllers.ManagerTask.TaskType;

public class RegistrationActivity extends CActivity implements ModuleChangedListener{
	public RegistrationActivity(Container parent) {
		this(RegistrationType.CUSTOMER,parent);
	}
	public RegistrationActivity(RegistrationType type,Container parent){
		super("REGISTRATION_ACTIVITY",parent);
		this.type=type;
		if (type==RegistrationType.CUSTOMER) {
			this.setCBarVisible(false);
			this.setCBarQueryesEnabled(false);
		}
	}
	@Override
	public void onData(TaskType task, Object data) {
			switch (type){
				case CUSTOMER:{register.setNextActivity(new LoginActivity(this.getParentContainer()));break;}
				default:{register.setNextActivity(new SpringBoardActivity(this.getParentContainer()));break;}
			}
		register.toNextActivity();
		register.setEnabled(true);
	}
	@Override
	public void onError(TaskType task, Exception e) {
		if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.UNKNOW_ERROR.name())){
			this.showCOPtionPane(MessageType.ERROR, CWords.getWord(this.getName()+"_"+e.getMessage()));
		}else{
			this.showCOPtionPane(MessageType.ERROR, CWords.getWord(e.getMessage()));
		}
	}
	@Override
	public void onModuleChange(Module<?> mod) {
		if (mod.getTag(false).equalsIgnoreCase("CUSTOMER_TYPE")||mod.getTag(false).equalsIgnoreCase("CONTRACT_TYPE")){
			this.add(new JScrollPane(user.getPanel()));
		}
	}
	@Override
	public void onStart() {
		if (this.type==RegistrationType.EMPLOYEE)
			user = new MUser(null, "EMPLOYEE_REGISTRATION", "EMPLOYEE_REGISTRATION_DESC", ViewType.WRITABLE,MUser.EMPLOYEE);
		else
			user= new MUser(null, "CUSTOMER_REGISTRATION", "CUSTOMER_REGISTRATION_DESC", ViewType.WRITABLE,MUser.CUSTOMER);
		user.addModuleChangedListener(this);
		render();
	}
	private void render(){
		register = new CActivityButton("BUTTON_REGISTER", this){
			public void onClick() {
				register.inibite();
				register.setEnabled(false);
				switch (type){
				case EMPLOYEE:{
					EmployeeContract emp=(EmployeeContract) user.getContent()[0];
					Employee usr=new Employee(emp.getPerson(), emp.getRole(),"", emp.getPerson().getBirthDate(), emp.getWages());
					try{
						Checks.checkEmployeeContract(emp);
						Checks.checkUser((String)user.getContent()[1], (String)user.getContent()[2],true);
						RegistrationActivity.this.putTask(TaskType.USER_REGISTER, usr, (String)user.getContent()[1], (String)user.getContent()[2]);
						RegistrationActivity.this.putTask(TaskType.EMPLOYEE_CONTRACT_SEND, emp);
					}catch(Exception e){onError(TaskType.EMPLOYEE_CONTRACT_SEND, e);}
					break;
				}
				default:{RegistrationActivity.this.putTask(TaskType.USER_REGISTER, (User)user.getContent()[0], (String)user.getContent()[1], (String)user.getContent()[2]);break;}
			}
			}	
			private static final long serialVersionUID = 1L;
		};
		cancel = new CActivityButton("BUTTON_CANCEL", this){
			public void onClick() {
				switch (type){
					case CUSTOMER:{cancel.setNextActivity(new LoginActivity(getParentContainer()));break;}
					default:{cancel.setNextActivity(new SpringBoardActivity(getParentContainer()));break;}
				}	
				cancel.toNextActivity();
			}	
			private static final long serialVersionUID = 1L;
		};		
		user.addWritableButton(register, cancel);
		CPanel renderedPanel=new CPanel(new BorderLayout());
		renderedPanel.add(new JScrollPane(user.getPanel()),BorderLayout.CENTER);
		super.add(renderedPanel);
	}
	public enum RegistrationType{EMPLOYEE,CUSTOMER}
	private RegistrationType type;
	private MUser user;
	private CActivityButton register, cancel;
	private static final long serialVersionUID = 1L;
}
